<?php

namespace app\web\controller;

use think\Controller;
use think\Db;

use dousdk\AuthAppDouyin;
use commonsdk\Tools;

class Video extends Controller {
    
      public $sysset;
    
    public function __construct(\think\App $app = null) {
        parent::__construct($app);

        $this->sysset = get_sysset();
    }

    public function index() {

        $_GPC = input();
        ignore_user_abort(true);
        set_time_limit(0);

        Db::name('sale_video')->where(['sale_id' => $_GPC['sale_id'], 'member_id'=> $_GPC['mid']])->delete();

        $sale = pdo_get('sale', array('id' => $_GPC['sale_id']));
        $user = pdo_get('member', array('id' => $_GPC['mid']));
        $video = json_decode($sale['video'], true);
        $video['delay'] = intval($video['delay']);
        if ($video['delay'] > 0) {
            $_GPC['time'] = intval($_GPC['time']) ? intval($_GPC['time']) : time();
            $time = time() - $_GPC['time'];
            if ($video['delay'] > $time) {
                sleep(min(30, $video['delay']));
                $_W['baseurl'] = $this->request->domain(true);
                $url = $_W['baseurl'] . '/web/video&mid=' . $_GPC['mid'] . '&sale_id=' .
                        $_GPC['sale_id'] . '&access_token=' . $_GPC['access_token'] .
                        '&idx=' . $_GPC['idx'] .
                        '&time=' . $_GPC['time'];
                asyn_http_get($url);
                exit;
            }
        }
        if (isset($video['list']) && is_array($video['list'])) {
            $videoidx = intval($_GPC['idx']);
            $video['title'] = $video['list'][$videoidx]['title'] ? $video['list'][$videoidx]['title'] : $data['title'];
            $video['path'] = $video['list'][$videoidx]['path'] ? $video['list'][$videoidx]['path'] : '';
        } else {
            $video['title'] = $video['title'] ? $video['title'] : $data['title'];
            $video['path'] = $video['path'] ? $video['path'] : '';
        }
        $path = $_SERVER['DOCUMENT_ROOT'] . $video['path'];
        if (!file_exists($path)) {
            exit;
        }
        $video_file = file_get_contents($path);
        $path .= time() . '.mp4';
        file_put_contents($path, $video_file . time());
        
        $dy = new AuthAppDouyin;
        $dy->client_key = $this->sysset['basic']['client_key'];
        $dy->client_secret = $this->sysset['basic']['client_secret'];
        
         $authAccount = [
            'open_id' => $user['openid'] ,
            'token' => $_GPC['access_token'],
        ];
        
        $video['path']=$path;
         
        $url = get_redirect_url($sale['poi_share']);
                if ($url) {
                    $params = convertUrlQuery($url);
                }
        $video['text'] =$video['title'];
                if ($sale['real_openid']) {
                    $video['at_users'] = [$sale['real_openid']];
                }
                if ($sale['micro_app_id']) {
                    $video['micro_app_id'] = $sale['micro_app_id'];
                }
                if ($sale['micro_app_url']) {
                    $video['micro_app_url'] = $sale['micro_app_url'];
                }
                if ($sale['micro_app_title']) {
                    $video['micro_app_title'] = $sale['micro_app_title'];
                }
                if (isset($params['id'])) {
                    $video['poi_id'] = $params['id'];
                }
                
              //  print_r($video);exit;
        
        $result=  $dy->publishItem($authAccount, $video);
        

        pdo_insert('member_video', array('member_id' => $_GPC['mid'], 'member' => json_encode($user), 'video' => json_encode($video), 'sale_id' => $_GPC['sale_id'], 'item_id' => $result['item_id'], 'createtime' => time()));
        unlink($path);
        exit;
    }

    public function verify() {

        $_GPC = input();
        ignore_user_abort(true);
        set_time_limit(0);
        sleep(3);
        $data = pdo_get('sale_video', array('sale_id' => $_GPC['sale_id'], 'member_id' => $_GPC['mid']));
        if (isset($data['url'])) {
            ihttp_get($data['url']);
            Db::name('sale_video')->where(['id'=>$data['id']])->delete();
        }
    }

}
